package dtaskboard.client.project;

import java.util.Collection;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.dto.RoleDTO;
import dtaskboard.client.login.LoginPage;
import dtaskboard.client.project.dialog.CloseSprintDialog;
import dtaskboard.client.project.dialog.CreateSprintDialog;
import dtaskboard.client.project.dialog.EditProjectDialog;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;
import dtaskboard.client.toolbar.ToolbarUtil;
import dtaskboard.client.util.OpenDialogCommand;

public class ProjectToolbar extends MenuBar {

  private static final Command EMPTY_COMMAND = new EmptyCommand();
  private final MenuItem projectItem;
  private MenuItem editProject;
  private MenuItem createSprint;
  private MenuItem closeSprint;
  private final String projectName;

  public ProjectToolbar(final String projectName) {
    this.projectItem = new MenuItem(projectName, ProjectToolbar.EMPTY_COMMAND);
    this.projectName = projectName;
    this.addItem(this.projectItem);

    LoginServiceAsync loginService = GWT.create(LoginService.class);
    String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
    loginService.getUserRoles(sessionId,
        new AsyncCallback<Collection<RoleDTO>>() {

          @Override
          public void onFailure(Throwable caught) {
            new LoginPage();
          }

          @Override
          public void onSuccess(Collection<RoleDTO> roles) {
            Panel toolbarPanel = RootPanel.get(Constants.projectToolbar);
            toolbarPanel.clear();

            if (ToolbarUtil.checkManagerRole(roles, projectName)) {
              ProjectToolbar.this.addEditProjectItem();
              ProjectToolbar.this.addCreateSprintItem();
              ProjectToolbar.this.addCloseSprintItem();
            }
            toolbarPanel.add(ProjectToolbar.this);
          }

        });
  }

  private void addEditProjectItem() {
    this.editProject =
        new MenuItem("Editar projeto", new OpenDialogCommand(
            new EditProjectDialog(this.projectName)));
    this.addItem(this.editProject);
  }

  private void addCreateSprintItem() {
    this.createSprint =
        new MenuItem("Criar Sprint", new OpenDialogCommand(
            new CreateSprintDialog(this.projectName)));
    this.addItem(this.createSprint);
  }

  private void addCloseSprintItem() {
    this.closeSprint =
        new MenuItem("Encerrar sprint", new OpenDialogCommand(
            new CloseSprintDialog(this.projectName)));
    this.addItem(this.closeSprint);
  }

}


class EmptyCommand implements Command {

  @Override
  public void execute() {
    // nao faz nada
  }

}
